export default {
  result(request) {
    const that = this;
    return new Promise((resolve, reject) => {
      request
        .then(res => {
          const data = res.data
          if (Number(res.status) === 200 && data.code === 1) {
            resolve(data.result)
          } else {
            resolve(data)
            data.msg && that.success(data.msg)
          }
        })
        .catch(err => {
          console.log('network err')
        })
    });
  },

  success(msg) {
    let div = document.createElement('div');
    div.id = 'success';
    div.className = 'success-box';
    div.innerHTML = msg;
    let body = document.getElementsByTagName('body')[0]
    body.appendChild(div)
    div.className = `${div.className} success-box-show`

    setTimeout(() => body.removeChild(div), 3000)
  },

  params(url) {
    return new Promise((resolve, reject) => {
      if (!url) {
        return resolve({})
      }
      let arr = url.split('&');
      let opt = {}
      for (let val of arr) {
        let list = val.split('=')
        opt[list[0]] = list[1]
      }
      resolve(opt)
    })
  }
}